import Vue from 'vue'
import Vuex from 'vuex'
import { getItem, setItem } from '@/utils/storage'

Vue.use(Vuex)
const USER_KEY = 'toutiao_user'
export default new Vuex.Store({
  state: {
    userToken: getItem(USER_KEY), // 当前用户登录状态信息
    cachePages: ['LayoutIndex']
  },
  mutations: {
    SET_TOKEN (state, data) {
      state.userToken = data
      setItem(USER_KEY, data)
    },
    // 添加缓存页面
    addCachePage (state, pageName) {
      if (!state.cachePages.includes(pageName)) {
        state.cachePages.push(pageName)
      }
    },
    // 移除缓存页面
    removeCachePage (state, pageName) {
      if (state.cachePages.includes(pageName)) {
        let index = state.cachePages.indexOf(pageName)
        // let index = state.cachecPages.findIndex(item => { item = pageName })
        state.cachePages.splice(index, 1)
        console.log(state.cachePages)
      }
    }
  },
  actions: {
  },
  modules: {
  }
})
